package co.yixiang.yshop.module.order.controller.admin.storeorderelectronics;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.order.controller.admin.storeorderelectronics.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.storeorderelectronics.StoreOrderElectronicsDO;
import co.yixiang.yshop.module.order.convert.storeorderelectronics.StoreOrderElectronicsConvert;
import co.yixiang.yshop.module.order.service.storeorderelectronics.StoreOrderElectronicsService;

@Tag(name = "管理后台 - 订单电子面单记录")
@RestController
@RequestMapping("/order/store-order-electronics")
@Validated
public class StoreOrderElectronicsController {

    @Resource
    private StoreOrderElectronicsService storeOrderElectronicsService;

    @PostMapping("/create")
    @Operation(summary = "创建订单电子面单记录")
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:create')")
    public CommonResult<Long> createStoreOrderElectronics(@Valid @RequestBody StoreOrderElectronicsCreateReqVO createReqVO) {
        return success(storeOrderElectronicsService.createStoreOrderElectronics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单电子面单记录")
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:update')")
    public CommonResult<Boolean> updateStoreOrderElectronics(@Valid @RequestBody StoreOrderElectronicsUpdateReqVO updateReqVO) {
        storeOrderElectronicsService.updateStoreOrderElectronics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单电子面单记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:delete')")
    public CommonResult<Boolean> deleteStoreOrderElectronics(@RequestParam("id") Long id) {
        storeOrderElectronicsService.deleteStoreOrderElectronics(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单电子面单记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:query')")
    public CommonResult<StoreOrderElectronicsRespVO> getStoreOrderElectronics(@RequestParam("id") Long id) {
        StoreOrderElectronicsDO storeOrderElectronics = storeOrderElectronicsService.getStoreOrderElectronics(id);
        return success(StoreOrderElectronicsConvert.INSTANCE.convert(storeOrderElectronics));
    }

    @GetMapping("/list")
    @Operation(summary = "获得订单电子面单记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:query')")
    public CommonResult<List<StoreOrderElectronicsRespVO>> getStoreOrderElectronicsList(@RequestParam("ids") Collection<Long> ids) {
        List<StoreOrderElectronicsDO> list = storeOrderElectronicsService.getStoreOrderElectronicsList(ids);
        return success(StoreOrderElectronicsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单电子面单记录分页")
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:query')")
    public CommonResult<PageResult<StoreOrderElectronicsRespVO>> getStoreOrderElectronicsPage(@Valid StoreOrderElectronicsPageReqVO pageVO) {
        PageResult<StoreOrderElectronicsDO> pageResult = storeOrderElectronicsService.getStoreOrderElectronicsPage(pageVO);
        return success(StoreOrderElectronicsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单电子面单记录 Excel")
    @PreAuthorize("@ss.hasPermission('order:store-order-electronics:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreOrderElectronicsExcel(@Valid StoreOrderElectronicsExportReqVO exportReqVO,
                                                 HttpServletResponse response) throws IOException {
        List<StoreOrderElectronicsDO> list = storeOrderElectronicsService.getStoreOrderElectronicsList(exportReqVO);
        // 导出 Excel
        List<StoreOrderElectronicsExcelVO> datas = StoreOrderElectronicsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "订单电子面单记录.xls", "数据", StoreOrderElectronicsExcelVO.class, datas);
    }

}
